package com.example.api.controller.admin;

import com.example.api.constant.StatusConst;
import com.example.api.dto.FoodDTO;
import com.example.api.exception.ParamException;
import com.example.api.query.FoodQuery;
import com.example.api.service.FoodService;
import com.example.api.utils.PageResult;
import com.example.api.utils.Result;
import com.example.api.vo.FoodVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "菜品接口")
@RestController
@RequestMapping("/admin/food")
public class FoodController {

    @Resource
    FoodService foodService;

    @ApiOperation("查询菜品")
    @GetMapping
    public Result<List<FoodVO>> find(FoodQuery foodQuery){ //CategoryQuery封装了查询的条件
        PageResult<FoodVO> page = foodService.find(foodQuery);

        return Result.ok("成功",page.getList(),page.getTotal());
    }

    @ApiOperation("添加菜品")
    @PostMapping
    public Result add(FoodDTO foodDTO){
        foodService.add(foodDTO);
        return Result.ok("添加成功");
    }


    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result findById(@PathVariable long id){
        FoodVO foodVO = foodService.findById(id);
        return Result.ok("查询成功",foodVO);
    }

    @ApiOperation("编辑菜品")
    @PutMapping("/{id}")
    public Result update(@PathVariable long id,FoodDTO foodDTO){ //@RequestBody将前端传递的json格式的请求参数转为对象
        foodService.update(id,foodDTO);
        return Result.ok("修改成功");
    }

    @ApiOperation("禁用启动菜品")
    @PutMapping("/{id}/status")
    public Result updateStatus(@PathVariable long id,String status){
        if(StatusConst.INACTIVE_STATUS_NAME.equals(status)){
            foodService.updateStatus(id,StatusConst.INACTIVE_STATUS);
        }else if(StatusConst.ACTIVE_STATUS_NAME.equals(status)){
            foodService.updateStatus(id,StatusConst.ACTIVE_STATUS);
        }else{
            throw new ParamException("参数错误");
        }
        return Result.ok("修改状态成功");
    }

//    @ApiOperation("删除菜品")
//    @DeleteMapping("/{id}")
//    public Result deleteById(@PathVariable long id){
//        foodService.deleteById(id);
//        return Result.ok("删除成功");
//    }
}
